app.controller("loginController", function($scope, $location, $window, localService,$http) {
	$scope.userData = [{
		"id": 1,
		"account": "a",
		"password": "123"
	}, {
		"id": 2,
		"account": "admin",
		"password": "123456"
	}, {
		"id": 3,
		"account": "administator",
		"password": "administator123."
	}];

	//表单合法才能点击提交按钮
	$scope.isdisabled = true;

	$scope.$watch('user.account', function(newVal, oldVal) {
		if(newVal == '') {
			$scope.nameMessage = '账号不能为空！';
		} else {
			$scope.nameMessage = '';
		}
		if(newVal != null && $scope.user.password != null) {
			$scope.isdisabled = false;
		} else {
			$scope.isdisabled = true;
		}
	});

	$scope.$watch('user.password', function(newVal, oldVal) {
		if(newVal == '') {
			$scope.passwordMessage = '密码不能为空！';
		} else {
			$scope.passwordMessage = '';
		}
		if(newVal != null && $scope.user.account != null) {
			$scope.isdisabled = false;
		} else {
			$scope.isdisabled = true;
		}
	});

	$scope.login = function() {
//		var account = $scope.user.username;
//		var password = $scope.user.password;
		//username=admin&password=123&submit=Login
//		$http({
//			method: 'post',
//			url: '/login',
//			data: {
//				"username":account,
//				"password":password,
//				"submit":"Login"
//			},
//		}).then(function successCallback(response) {
//			console.log(response.status);
//			$location.path("/mainPage");
//		});
	
		var account = $scope.user.account;
		var password = $scope.user.password;
		var flag = false;
		angular.forEach($scope.userData, function(value, key) {
			if(value.account != undefined && value.account === account && value.password === password) {
				flag = true;
			}
		});
		if(flag) {

			$location.path("/mainPage");
		} else {
			alert("账号或密码不正确!")
		};
	}

	$scope.enter = function(e) {
		var keycode = window.event ? e.keyCode : e.which;
		if(keycode == 13) { //回车
			$scope.login();
		}
	}

});